package net.com.sunkey.messagehandler;

import java.nio.ByteBuffer;

import net.com.sunkey.protobuf.Protocol;
import net.com.sunkey.protobuf.CertificateResponse.CertificateResponseData;
import net.com.sunkey.serverlogic.Server;
import net.com.sunkey.util.Certificate;

/**
 * @author kohachiro
 *
 */
public class CertificateResponseHandler {
	/* (non-Javadoc)
	 * @see com.sunkey.tdserver.messagehandler.MessageHandler#invoke(java.lang.Object, org.jboss.netty.channel.Channel)
	 */
	//@Override
	public static int invoke(Object data, Object channel) throws Exception {
		System.out.println("["+channel.hashCode()+"]["+Protocol.MessageHandler.CertificateResponse_VALUE+"]CertificateResponse");
		CertificateResponseData.Builder builder=CertificateResponseData.newBuilder();
		builder.setDelay((int)(System.currentTimeMillis()-((ByteBuffer)data).getLong())>>1);
		builder.setTime(System.currentTimeMillis());		
		builder.setCertificate(Certificate.gen());
		CertificateResponseData message=builder.build();
		
		Server.sendResponse(Protocol.MessageHandler.CertificateResponse_VALUE, ByteBuffer.wrap(message.toByteArray()), channel);

		return Protocol.MessageHandler.CertificateResponse_VALUE;
	}

}
